import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/showRequest")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(req.getProtocol());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getMethod());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getRequestURI());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getContextPath());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getQueryString());
        stringBuffer.append("<br>");

        Enumeration<String> headerName = req.getHeaderNames();
        while (headerName.hasMoreElements()) {
            String name = headerName.nextElement();
            stringBuffer.append(name + ": " + req.getHeader(name));
            stringBuffer.append("<br>");
        }
        resp.setContentType("text/html; charset=utf8");//header必须在body前面设置
        resp.getWriter().write(stringBuffer.toString());
    }
}
